﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Capture;
using Capture.Hook;
using Capture.Interface;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace OsdTempGUI
{
    public partial class formMain : Form
    {
        public formMain()
        {
            InitializeComponent();
        }
            
        CaptureProcess _captureProcess;
        Process _process;
        int processID = 0;

        private void buttonInject_Click(object sender, EventArgs e)
        {
            if (_captureProcess == null)
            {
                buttonInject.Enabled = false;
                AttachProcess();
            }
            else
            {
                HookManager.RemoveHookedProcess(_captureProcess.Process.Id);
                _captureProcess.CaptureInterface.Disconnect();
                _captureProcess = null;
            }

            if (_captureProcess != null)
            {
                buttonInject.Text = "Disconnetti";
                buttonInject.Enabled = true;
            }
            else
            {
                buttonInject.Text = "Inietta";
                buttonInject.Enabled = true;
            }
        }

        private void AttachProcess()
        {
            string exeName = Path.GetFileNameWithoutExtension(textboxProcessName.Text);
            Process[] processes = Process.GetProcessesByName(exeName);

            foreach (Process singleProcess in processes)
            {
                //Iniettiamo il primo processo che troviamo

                //Se il processo non ha una MainWindowHandle, lo saltiamo (ci serve per metterlo in primo piano e cazzi&mazzi)
                if (singleProcess.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                //Lo saltiamo se è già hookato (casomai vogliamo iniettare più processi con lo stesso nome)
                if (HookManager.IsHooked(singleProcess.Id))
                {
                    continue;
                }

                Direct3DVersion directXVersion = Direct3DVersion.AutoDetect;

                if (radiobuttonDirectX11.Checked)
                {
                    directXVersion = Direct3DVersion.Direct3D11;
                }
                else if (radiobuttonDirectX10_1.Checked)
                {
                    directXVersion = Direct3DVersion.Direct3D10_1;
                }
                else if (radiobuttonDirectX10.Checked)
                {
                    directXVersion = Direct3DVersion.Direct3D10;
                }
                else if (radiobuttonDirectX9.Checked)
                {
                    directXVersion = Direct3DVersion.Direct3D9;
                }
                else if (radiobuttonDirectXAuto.Checked)
                {
                    directXVersion = Direct3DVersion.AutoDetect;
                }

                CaptureConfig cc = new CaptureConfig();
                cc.Direct3DVersion = directXVersion;
                cc.ShowOverlay = true;

                processID = singleProcess.Id;
                _process = singleProcess;

                CaptureInterface captureInterface = new CaptureInterface();
                captureInterface.RemoteMessage += new MessageReceivedEvent(CaptureInterface_RemoteMessage);
                _captureProcess = new CaptureProcess(singleProcess, cc, captureInterface);

                break;
            }

            Thread.Sleep(10);

            if (_captureProcess == null)
            {
                MessageBox.Show("Non sono stati trovati processi di nome: \"" + exeName + "\".", "Attenzione!");
            }
        }

        private void CaptureInterface_RemoteMessage(MessageReceivedEventArgs message)
        {
            textboxLog.Invoke(new MethodInvoker(delegate()
            {
                textboxLog.Text = String.Format("{0}" + System.DateTime.Now.Hour.ToString("00") + ":" +
                System.DateTime.Now.Minute.ToString("00") + ":" + System.DateTime.Now.Second.ToString("00") +
                " - {1}\r\n", textboxLog.Text, message);
            }
                ));
        }

        private void textboxLog_TextChanged(object sender, EventArgs e)
        {
            textboxLog.SelectionLength = 0;
            textboxLog.SelectionStart = textboxLog.Text.Length;
            textboxLog.ScrollToCaret();
        }

        private void buttonCercaProcesso_Click(object sender, EventArgs e)
        {
            formProcessList formProcess = new formProcessList(textboxProcessName);
            formProcess.Show();
        }
    }
}
